package GUI;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import Exceptions.StrategoException;
import Game.GameCenter;
import Game.IGameCenter;
import Game.IGameInfo;
import Logic.IPlayEngine;
import Logic.Objects.GameOptions;
import com.cloudgarden.resource.SWTResourceManager;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class MainGui extends org.eclipse.swt.widgets.Composite {

	//private static final String GAME_SERVER = GameCenterRemote.LOCAL_SERVER;
	static String fBgMusic = System.getProperty("user.dir")+"/src/Resources/stratego.mp3";
	static String fClickSound= System.getProperty("user.dir")+"/src/Resources/explosion.mp3";
	public Label label1;
	public ProgressBar progressBar1;
	private Button button3;
	private Text text1;
	private Button button1;
	public IGameCenter myGC;
	public String UserName;
	public IPlayEngine myPE = null;
	public String GameName="";
	public int nPlayers=0;			
	public int nAI=0;
	public int ModeFlag;
	public GameOptions gameOptions;
	public Shell shell;
	private Button button4;
	private Text text4;
	private Text label3;
	private Button button7;
	private Button button6;
	private Text text2;
	private Label label2;
	private Canvas canvas4;
	private Canvas canvas3;
	private Button button5;
	private Canvas canvas2;
	private Canvas canvas1;
	private String Password;
	static MP3 bgMusic;
	static Display display;

	{
		//Register as a resource user - SWTResourceManager will
		//handle the obtaining and disposing of resources
		SWTResourceManager.registerResourceUser(this);
	}

	public MainGui(Composite parent, int style) {
		super(parent, style);
		shell=(Shell) parent;
		initGUI();
		myGC=new GameCenter();
	}

	/**
	 * Initializes the GUI.
	 */
	private void initGUI() {
		this.setSize(892, 356);
		this.setBackground(SWTResourceManager.getColor(192, 192, 192));
		FormLayout thisLayout = new FormLayout();
		this.setLayout(thisLayout);
		this.setBackgroundMode(0);
		{
			FormData canvas2LData = new FormData();
			canvas2LData.left =  new FormAttachment(0, 1000, 1);
			canvas2LData.top =  new FormAttachment(0, 1000, 110);
			canvas2LData.width = 893;
			canvas2LData.height = 245;
			canvas2 = new Canvas(this, SWT.NONE);
			canvas2.setLayoutData(canvas2LData);
			canvas2.setBackgroundImage(SWTResourceManager.getImage("Resources/stratego_middle.jpg"));
			{
				label1 = new Label(canvas2, SWT.NONE);
				label1.setText("Enter Your Stratego User Name and Press Connect to Game Center...");
				label1.setFont(SWTResourceManager.getFont("Segoe UI", 9, 3, false, false));
				label1.setBounds(0, 226, 532, 19);
				label1.setForeground(SWTResourceManager.getColor(255, 0, 0));
			}
			{
				progressBar1 = new ProgressBar(canvas2, SWT.NONE);
				progressBar1.setBounds(0, 221, 892, 6);
				//progressBar1.setVisible(false);
			}
			{
				canvas3 = new Canvas(canvas2, SWT.NONE);
				canvas3.setBounds(7, 0, 302, 164);
				canvas3.setBackgroundImage(SWTResourceManager.getImage("Resources/stock3.jpg"));
				{
					text1 = new Text(canvas3, SWT.NONE);
					text1.setOrientation(SWT.HORIZONTAL);
					text1.setBounds(35, 6, 102, 29);
					text1.setText("Admin");
					text1.setFont(SWTResourceManager.getFont("Segoe UI", 10, 2, false, false));
				}
				{
					button1 = new Button(canvas3, SWT.PUSH | SWT.CENTER);
					button1.setText("Login");
					button1.setFont(SWTResourceManager.getFont("Arial", 7, 1, false, false));
					button1.setBounds(14, 45, 126, 29);
					button1.setBackgroundImage(SWTResourceManager.getImage("Resources/src3.jpg"));
					button1.addMouseListener(new MouseAdapter() {
						public void mouseDown(MouseEvent evt) {
							button1MouseDown(evt);
						}
					});
				}
				{
					button3 = new Button(canvas3, SWT.PUSH | SWT.CENTER);
					button3.setText("Start Game!");
					button3.setBounds(15, 87, 272, 31);
					button3.setFont(SWTResourceManager.getFont("Arial", 9, 1, false, false));
					button3.setBackgroundImage(SWTResourceManager.getImage("Resources/src3.jpg"));
					//button3.setVisible(false);
					button3.setEnabled(false);
					button3.addMouseListener(new MouseAdapter() {
						public void mouseDown(MouseEvent evt) {
							button3MouseDown(evt);
						}
					});
					//button3.setVisible(false);
				}
				{
					button5 = new Button(canvas3, SWT.PUSH | SWT.CENTER);
					button5.setText("High Scores");
					button5.setBounds(15, 126, 125, 31);
					button5.setFont(SWTResourceManager.getFont("Arial",9,1,false,false));
					button5.setBackgroundImage(SWTResourceManager.getImage("Resources/src3.jpg"));
					button5.addMouseListener(new MouseAdapter() {
						public void mouseDown(MouseEvent evt) {
							button5MouseDown(evt);
						}
					});
					button5.setEnabled(false);
				}
				{
					text2 = new Text(canvas3, SWT.NONE);
					text2.setText("123");
					text2.setBounds(195, 5, 92, 30);
					text2.setEchoChar('*');
				}
				{
					button6 = new Button(canvas3, SWT.PUSH | SWT.CENTER);
					button6.setText("Signup");
					button6.setBounds(164, 46, 126, 29);
					button6.setFont(SWTResourceManager.getFont("Arial", 9, 1, false, false));
					button6.setBackgroundImage(SWTResourceManager.getImage("Resources/src3.jpg"));
					button6.addMouseListener(new MouseAdapter() {
						public void mouseDown(MouseEvent evt) {
							button6MouseDown(evt);
						}
					});
				}
				{
					button7 = new Button(canvas3, SWT.PUSH | SWT.CENTER);
					button7.setText("Help");
					button7.setBounds(164, 126, 126, 31);
					button7.addMouseListener(new MouseAdapter() {
						public void mouseDown(MouseEvent evt) {
							button7MouseDown(evt);
						}
					});
				}
				{
					label3 = new Text(canvas3, SWT.NONE);
					label3.setText("ID");
					label3.setBounds(13, 6, 22, 29);
				}
				{
					text4 = new Text(canvas3, SWT.NONE);
					text4.setText("Pass");
					text4.setBounds(163, 5, 32, 30);
				}
			}
			{
				button4 = new Button(canvas2, SWT.PUSH | SWT.CENTER);
				button4.setText("Quit");
				button4.setBounds(814, 185, 60, 30);
				button4.addMouseListener(new MouseAdapter() {
					public void mouseDown(MouseEvent evt) {
						button4MouseDown(evt);
					}
				});
			}

		}
		{
			FormData canvas1LData = new FormData();
			canvas1LData.left =  new FormAttachment(0, 1000, 1);
			canvas1LData.top =  new FormAttachment(0, 1000, -3);
			canvas1LData.width = 891;
			canvas1LData.height = 113;
			canvas1 = new Canvas(this, SWT.NONE);
			canvas1.setLayoutData(canvas1LData);
			canvas1.setBackgroundImage(SWTResourceManager.getImage("Resources/stratego_banner.jpg"));
			{
				canvas4 = new Canvas(canvas1, SWT.NONE);
				canvas4.setBounds(70, 81, 152, 37);
				canvas4.setBackgroundImage(SWTResourceManager.getImage("Resources/stock3.jpg"));
				{
					label2 = new Label(canvas4, SWT.NONE);
					label2.setText("Main Menu");
					label2.setBounds(17, 6, 118, 25);
					label2.setBackground(SWTResourceManager.getColor(192, 192, 192));
					label2.setAlignment(SWT.CENTER);
					label2.setForeground(SWTResourceManager.getColor(54, 54, 54));
					label2.setFont(SWTResourceManager.getFont("Arial", 13, 3, false, false));
				}
			}
		}
		this.layout();
	}

	/**
	 * Auto-generated main method to display this 
	 * org.eclipse.swt.widgets.Composite inside a new Shell.
	 */
	public static void main(String[] args) {
		bgMusic = new MP3(fBgMusic);
		//bgMusic.play();

		display = Display.getDefault();
		Shell shell = new Shell(display);
		shell.setBackgroundMode(SWT.INHERIT_DEFAULT); 
		MainGui inst = new MainGui(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		Cursor cursor = new Cursor(display, new Image(display,System.getProperty("user.dir")+"/src/Resources/cursor.gif").getImageData(), 0, 0);
	    shell.setCursor(cursor);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		bgMusic.close();
	}

	private void button1MouseDown(MouseEvent evt) {
		MP3 clickSound = new MP3(fClickSound);
		//clickSound.play();
		UserName=text1.getText();
		Password=text2.getText();
		//myGC=GameCenterRemote.getGameCenter(GAME_SERVER);
		//myGC=new GameCenter();
		try{
			if (UserName.equals("Admin") && Password.equals("123")){
				//clickSound.play();
				Shell shell2 = new Shell(getDisplay());
				//Importer inst = new Importer(shell2, SWT.NULL);
				Manager inst=new Manager(shell2,SWT.NULL);
				Point size = inst.getSize();
				shell2.setLayout(new FillLayout());
				shell2.layout();
				if(size.x == 0 && size.y == 0) {
					inst.pack();
					shell2.pack();
				} else {
					Rectangle shellBounds = shell2.computeTrim(0, 0, size.x, size.y);
					shell2.setSize(shellBounds.width, shellBounds.height);
				}
				Cursor cursor = new Cursor(display, new Image(display,System.getProperty("user.dir")+"/src/Resources/cursor.gif").getImageData(), 0, 0);
			    shell2.setCursor(cursor);
				shell2.open();
				while (!shell2.isDisposed()) {
					if (!getDisplay().readAndDispatch())
						getDisplay().sleep();
				}
			}
			else{
				label1.setText("Calling Game Center..");
				myGC.login(UserName, Password);
				label1.setText("Done");
				progressBar1.setSelection(33);
				button3.setEnabled(true);
				button5.setEnabled(true);
				text1.setEnabled(false);
				text2.setEnabled(false);
				button1.setEnabled(false);
				button6.setEnabled(false);
			}
		}
		catch(Exception e){
			label1.setText(e.getMessage());
		}
	}

	private void button3MouseDown(MouseEvent evt) {
		MP3 clickSound = new MP3(fClickSound);
		//clickSound.play();
		ModeFlag=0;

		Shell shell2 = new Shell(getDisplay());
		exGUI inst = new exGUI(shell2, SWT.NULL,this,0);
		Point size = inst.getSize();
		shell2.setLayout(new FillLayout());
		shell2.layout();
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell2.pack();
		} else {
			Rectangle shellBounds = shell2.computeTrim(0, 0, size.x, size.y);
			shell2.setSize(shellBounds.width, shellBounds.height);
		}
		Cursor cursor = new Cursor(display, new Image(display,System.getProperty("user.dir")+"/src/Resources/cursor.gif").getImageData(), 0, 0);
	    shell2.setCursor(cursor);
		shell2.open();
		inst.loadForm(null);
		while (!shell2.isDisposed()) {
			if (!getDisplay().readAndDispatch())
				getDisplay().sleep();
		}

		if (inst.Result) 
			StartGame();
	}

	private void StartGame(){
		MP3 clickSound = new MP3(fClickSound);	
		//clickSound.play();
		try{
			if (ModeFlag==0){ //Create Game
				myPE = myGC.CreateNewGame(GameName, nPlayers, nAI, UserName);
				myPE.setGameOptions(gameOptions);
			}
			myPE.strSetMySoldiersRandomlly(UserName);
			progressBar1.setSelection(100*myGC.getGameInfo(GameName).getCurrentNumOfPlayers()/nPlayers);
			if (myGC.getGameInfo(GameName).getCurrentNumOfPlayers()==nPlayers){
				label1.setText("Starting Game...");
				progressBar1.setSelection(100);
				bgMusic.close();
				GUI.start(myPE,UserName);
			}
		}
		catch (StrategoException ex) {label1.setText(ex.getMessage());}
		canvas4.setVisible(false);
	}

	private void button6MouseDown(MouseEvent evt) {
		MP3 clickSound = new MP3(fClickSound);
		//clickSound.play();
		label1.setText("Calling Game Center..");
		UserName=text1.getText();
		Password=text2.getText();
		//myGC=GameCenterRemote.getGameCenter();
		//myGC=new GameCenter();
		try{
			myGC.signup(UserName, Password);
			label1.setText("Done");
			progressBar1.setSelection(33);
			button3.setEnabled(true);
			button5.setEnabled(true);
			text1.setEnabled(false);
			text2.setEnabled(false);
			button1.setEnabled(false);
			button6.setEnabled(false);
		}
		catch(StrategoException e){
			label1.setText(e.getMessage());
		}
	}

	private void button7MouseDown(MouseEvent evt) { 
		try{	
			String AsmDir = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
			AsmDir = AsmDir.substring(1);
			FileReader fr = new FileReader(AsmDir+ "/Help/help.txt");
			BufferedReader myInput = new BufferedReader(fr);
			String s;
			StringBuffer b = new StringBuffer();
			while ((s = myInput.readLine()) != null) {
			b.append(s);
			b.append("\n");
			}

			String text = b.toString();
			Shell shell = new Shell(getDisplay());
			StyledText textArea = new StyledText(shell, SWT.BORDER|SWT.V_SCROLL|SWT.H_SCROLL);
			textArea.setMargins(20, 20, 20, 20);
			textArea.setSize(610, 620);
			textArea.setText(text);
			textArea.setBackground(SWTResourceManager.getColor(232, 232, 232));
			shell.pack();
			shell.open();
			
		} 
		catch (Exception e) {
			label1.setText("Error reading help text");
			e.printStackTrace();
			}
	}
	
	private void button5MouseDown(MouseEvent evt) {
		try{	
			String AsmDir = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
			AsmDir = AsmDir.substring(1);
			FileReader fr = new FileReader(AsmDir+ "/Help/help.txt");
			BufferedReader myInput = new BufferedReader(fr);
			String s;
			StringBuffer b = new StringBuffer();
			while ((s = myInput.readLine()) != null) {
			b.append(s);
			b.append("\n");
			}

			String text = b.toString();
			Shell shell = new Shell(getDisplay());
			StyledText textArea = new StyledText(shell, SWT.BORDER|SWT.V_SCROLL|SWT.H_SCROLL);
			textArea.setMargins(20, 20, 20, 20);
			textArea.setSize(610, 620);
			textArea.setText(text);
			textArea.setBackground(SWTResourceManager.getColor(232, 232, 232));
			shell.pack();
			shell.open();
			
		} 
		catch (Exception e) {
			label1.setText("Error reading help text");
			e.printStackTrace();
			}
	}
	
	private void button4MouseDown(MouseEvent evt) {
		shell.close();
		System.exit(0);
	}
}